﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class CommunityLogin : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.IsPostBack)
        {
            CheckLogin();
        }
    }
    private void CheckLogin()
    {
        this.LabelNotice.Text = "";
        if (Session["ImgCode"] == null) return;
        if (this.TextImgCode.Text.ToUpper() != (string)Session["ImgCode"])
        {
            this.LabelNotice.Text = "“验证码”输入有误。";
            return;
        }
        string UserName = this.TextUserName.Text;
        string Password = this.TextPassword.Text;

        SqlConnection MySqlConnection = new SqlConnection();
        MySqlConnection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["GoWLLConnectionString"].ConnectionString;
        MySqlConnection.Open();

        SqlCommand MySqlCommand = new SqlCommand();
        MySqlCommand.CommandType = CommandType.StoredProcedure;
        MySqlCommand.CommandText = "CheckLogin";
        MySqlCommand.Connection = MySqlConnection;
        MySqlCommand.Parameters.Add(new SqlParameter("@UserName", SqlDbType.VarChar, 20, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, UserName));
        MySqlCommand.Parameters.Add(new SqlParameter("@Password", SqlDbType.VarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, Password));
        MySqlCommand.Parameters.Add(new SqlParameter("@OK", SqlDbType.Bit, 1, ParameterDirection.Output, true, 0, 0, "", DataRowVersion.Proposed, null));
        MySqlCommand.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int, 4, ParameterDirection.Output, true, 0, 0, "", DataRowVersion.Proposed, null));
        Application.Lock();
        MySqlCommand.ExecuteNonQuery();
        Application.UnLock();
        if (!(bool)MySqlCommand.Parameters[2].Value)
        {
            MySqlConnection.Close();
            MySqlCommand.Dispose();
            MySqlConnection.Dispose();
            this.LabelNotice.Text = "登录失败，请重试。";
            return;
        }
        Session["CommunityUser"] = UserName;
        Session["CommunityUserID"] = (int)MySqlCommand.Parameters[3].Value;
        MySqlConnection.Close();
        MySqlCommand.Dispose();
        MySqlConnection.Dispose();
        Response.Redirect("CommunityList.aspx");
    }
    protected void ButtonLogin_Click(object sender, EventArgs e)
    {
        CheckLogin();
    }
}
